from django.db import models
from django.db.models import SET_NULL, ManyToManyField
from django.forms import DateField


# Create your models here.
class Staff(models.Model):
    id = models.CharField(max_length=10, primary_key=True)
    passwd = models.CharField(max_length=20, blank=True, null=True)
    name = models.CharField(max_length=10, blank=True, null=True)
    EntryTime = models.CharField(max_length=10, blank=True, null=True)
    WorkingState = models.CharField(max_length=10, blank=True, null=True)
    sex = models.CharField(max_length=10, blank=True, null=True)
    age = models.IntegerField(default=0, blank=True, null=True)
    email = models.EmailField(blank=True, null=True)
    Academic = models.CharField(max_length=50, blank=True, null=True)
    Experience = models.CharField(max_length=256, blank=True, null=True)
    phoneNum = models.CharField(max_length=11, blank=True, null=True)
    isLogin = models.BooleanField(default=False, blank=True)
    isRegister = models.BooleanField(default=False, blank=True)

    def to_dict3(self, fields='id', exclude=None):
        return get_dict(self, fields, exclude)

    def to_dict7(self, fields=('name', 'age', 'sex'), exclude=None):
        return get_dict(self, fields, exclude)





class Customer(models.Model):
    Username = models.CharField(max_length=20, primary_key=True)
    passwd = models.CharField(max_length=20)
    email = models.CharField(max_length=10, blank=True, null=True)

    def to_dict10(self, fields=None, exclude=None):
        return get_dict(self, fields, exclude)


def get_dict(self, fields=None, exclude=None):
    data = {}
    for f in self._meta.concrete_fields + self._meta.many_to_many:
        value = f.value_from_object(self)
        if fields and f.name not in fields:
            continue
        if exclude and f.name in exclude:
            continue
        if isinstance(f, ManyToManyField):
            value = [i.id for i in value] if self.pk else None
        if isinstance(f, DateField):
            value = value.strftime('%Y-%m-%d') if value else None
        data[f.name] = value
    return data


